package org.wishwork.api.allocate.upload.in;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wishwork.api.Api;
import org.wishwork.api.allocate.upload.in.request.AllocateInUploadRequest;
import org.wishwork.api.allocate.upload.in.request.Item;
import org.wishwork.context.Context;
import org.wishwork.helper.HttpHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: 库存调拨上传（仓内调拨）
 * @author: WangYX
 * @create: 2022-04-12 21:55
 * @Version: 1.0.0
 **/
public class AllocateInUploadApi extends Api<List<AllocateInUploadResponse>, AllocateInUploadRequest> {

    private static final Logger log = LoggerFactory.getLogger(AllocateInUploadApi.class);

    private Context context;

    public AllocateInUploadApi(Context context) {
        this.context = context;
    }

    @Override
    protected String query(AllocateInUploadRequest allocateInUploadRequest) throws Exception {
        String url = "/open/jushuitan/allocate/in/upload";

        Map<String, Object> params = new HashMap<>();
        params.put("io_date", allocateInUploadRequest.getIoDate());
        params.put("link_warehouse", allocateInUploadRequest.getLinkWarehouse());
        params.put("remark", allocateInUploadRequest.getRemark());
        params.put("so_id", allocateInUploadRequest.getSoId());
        params.put("warehouse", allocateInUploadRequest.getWarehouse());
        if (allocateInUploadRequest.getWmsCoId() != null) {
            params.put("wms_co_id", allocateInUploadRequest.getWmsCoId());
        }
        if (allocateInUploadRequest.getItems() != null && allocateInUploadRequest.getItems().size() > 0) {
            List<Map<String, Object>> list = new ArrayList<>();
            for (Item item : allocateInUploadRequest.getItems()) {
                Map<String, Object> map = new HashMap<>();
                map.put("qty", item.getQty());
                map.put("sku_id", item.getSkuId());
                if (StrUtil.isNotEmpty(item.getBatchId())) {
                    map.put("batch_id", item.getBatchId());
                }
                if (StrUtil.isNotEmpty(item.getProducedDate())) {
                    map.put("produced_date", item.getProducedDate());
                }
                if (StrUtil.isNotEmpty(item.getExpirationDate())) {
                    map.put("expiration_date", item.getExpirationDate());
                }
                list.add(map);
            }
            params.put("items", list);
        }
        return HttpHelper.sendRequest(params, url);
    }

    @Override
    protected List<AllocateInUploadResponse> process(JSONObject data, AllocateInUploadRequest allocateInUploadRequest) throws Exception {
        JSONArray datas = data.getJSONArray("datas");
        List<AllocateInUploadResponse> allocateInUploadResponses = datas.toJavaList(AllocateInUploadResponse.class);
        return allocateInUploadResponses;
    }
}
